package com.sodaopen.study.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.sodaopen.study.entity.Course;

import java.util.List;

public interface CourseMapper {

    @Select("select * from t_course t where t.stage_id=#{id} and t.public='Y' " + 
    		"and exists (select null from t_user u, t_clazz_course c where u.clazz_id=c.clazz_id and t.id=c.course_id and u.id=#{userId} )" + 
    		"order by sort")
    List<Course> selectByStageId(@Param("id") Integer id, @Param("userId") Integer userId);

    @Select("select * from t_course where id=#{id}")
	Course selectById(Integer id);

    @Insert("insert into t_course_finish(user_id, course_id) values(#{userId}, #{courseId})")
    void insertFinishRecord(@Param("userId")Integer userId, @Param("courseId")Integer courseId);

    @Select("select * from t_course order by stage_id, sort")
    List<Course> selectByParam();

    @Insert("insert into t_course(name, detail, pic, stage_id, teacher_id, sort) " +
            "values(#{name}, #{detail}, #{pic}, #{stageId}, #{teacherId}, #{sort})")
    void insertCourse(Course course);

    @Select("select ifnull(max(sort),0) from t_course where stage_id=#{stageId}")
    Integer selectMaxSort(Integer stageId);

    @Select("select * from t_course c where not exists "
    		+ "(select 1 from t_clazz_course cc where cc.course_id=c.id and cc.clazz_id=#{clazzId})"
    		+ " order by stage_id, sort")
	List<Course> selectClazzNotAssignCourse(Integer clazzId);
}
